package top.frsf.bullet;

import top.frsf.GameObject;
import top.frsf.GamePanel;
import top.frsf.envir.Blast;
import top.frsf.envir.Wall;
import top.frsf.tank.Bot;
import top.frsf.tool.Direction;

import java.awt.*;
import java.util.ArrayList;

/**
 * @BelongsProject: TankWar1
 * @BelongsPackage: top.frsf.bullet
 * @Author: FRSF
 * @CreateTime: 2022-09-03  11:07
 * @Description: 子弹类
 * @Version: 1.0
 */
public class Bullet extends GameObject {
    // 尺寸
    int width = 10;
    int height = 10;

    // 速度
    int speed = 7;

    // 方向
    Direction direction;


    public Bullet(String img, int x, int y, GamePanel gamePanel, Direction direction) {
        super(img, x, y, gamePanel);
        this.direction = direction;
    }

    @Override
    public void paintSelf(Graphics g) {
        g.drawImage(img, x, y, null);
        go();
        if (this.gamePanel.flashNum % 6 == 0) {
            hitBot();
            hitWall();
            hitBase();
            moveToBoard(x, y);
        }

    }

    @Override
    public Rectangle getRec() {
        return new Rectangle(x, y, width, height);
    }

    /* bullet move */
    public void upward() {
        y -= speed;
    }

    public void downward() {
        y += speed;
    }

    public void leftward() {
        x -= speed;
    }

    public void rightward() {
        x += speed;
    }

    public void go() {
        switch (direction) {
            case UP:
                upward();
                break;
            case DOWN:
                downward();
                break;
            case LEAF:
                leftward();
                break;
            case RIGHT:
                rightward();
                break;
        }
    }

    /* playerOne  子弹摧毁电脑tank检测 */
    public void hitBot() {
        ArrayList<Bot> bots = this.gamePanel.botList;
        for (Bot bot : bots) {
            if (this.getRec().intersects(bot.getRec())) {
                this.gamePanel.blastList.add(new Blast("", bot.x,
                        bot.y, this.gamePanel));
                this.gamePanel.botList.remove(bot);
                this.gamePanel.removeListBullets.add(this);
                break;
            }
        }

    }

    /* 子弹摧毁墙壁检测 */
    public void hitWall() {
        // 围墙列表便利
        ArrayList<Wall> wallList = this.gamePanel.wallList;
        for (Wall wall : wallList) {
            if (this.getRec().intersects(wall.getRec())) {
                wallList.remove(wall);
                this.gamePanel.removeListBullets.add(this);
                break;
            }
        }
    }

    /* 子弹摧毁基地检测 */
    public void hitBase() {
        ArrayList<Wall> mainList = this.gamePanel.mainList;
        for (Wall wall : mainList) {
            if (this.getRec().intersects(wall.getRec())) {
                mainList.remove(wall);
                this.gamePanel.removeListBullets.add(this);
                break;
            }
        }
    }

    /* 子弹出界 */
    public void moveToBoard(int x, int y) {
        if (x < 0 || y < 0 || x > this.gamePanel.getWidth() || y > this.gamePanel.getHeight())
            this.gamePanel.removeListBullets.add(this);
    }


}
